package com.ying.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.ying.util.RabbitMQUtils;

import java.io.IOException;

/**
 * ClassName: RoutingProducer
 * Package: com.ying.routing
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/3/27 15:13
 * @Version 1.0
 */
public class TopicProducer {
    private static final String EXCHANGE_NAME = "topicExchange";
    private static final String QUEUE_NAME = "topicQueue";

    public static void main(String[] args) throws IOException {
        //1.建立连接
        Connection connection = RabbitMQUtils.getConnection("192.168.160.134", 5672, "myHV",
                "testUser", "123456");

        //2.获取channel
        Channel channel = connection.createChannel();
        //3。获取交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true,false,null);
        //4.创建队列
        for (int i = 1;i <= 2;i ++)
        {
            channel.queueDeclare(QUEUE_NAME + i,true,false,false,null);
        }
        //5.交换机和队列绑定
        channel.queueBind(QUEUE_NAME + 1,EXCHANGE_NAME,"user.*",null);
        channel.queueBind(QUEUE_NAME + 1,EXCHANGE_NAME,"pay.*",null);

        channel.queueBind(QUEUE_NAME + 2,EXCHANGE_NAME,"#.*",null);
        //6.发布消息
        channel.basicPublish(EXCHANGE_NAME,"user.info",null,("user.info日志").getBytes());
        channel.basicPublish(EXCHANGE_NAME,"user.waring",null,("user.waring日志").getBytes());
        channel.basicPublish(EXCHANGE_NAME,"pay.debug",null,("pay.debug日志").getBytes());
        channel.basicPublish(EXCHANGE_NAME,"order.info",null,("order.info日志").getBytes());
        //7.释放资源
        RabbitMQUtils.release(connection, channel);
    }
}
